import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerSalaryApi } from '#/api/per/salary';
import type { DescriptionItemSchema } from '#/components/description';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'year',
      label: '年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
      },
    },
    {
      fieldName: 'salary',
      label: '工资',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入工资',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'partTimeIncome',
      label: '兼职收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入工资',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'bonus',
      label: '奖金',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入工资',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'otherIncome',
      label: '其他收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入其他收入',
        addonAfter: '元',
      },
    },
    {
      fieldName: 'totalIncome',
      label: '合计收入',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '自动计算',
        addonAfter: '元',
      },
      dependencies: {
        triggerFields: ['salary', 'partTimeIncome', 'bonus', 'otherIncome'],
        readOnly: true,
        handle: (value, form) => {
          const salary = Number(form.salary) || 0;
          const partTimeIncome = Number(form.partTimeIncome) || 0;
          const bonus = Number(form.bonus) || 0;
          const otherIncome = Number(form.otherIncome) || 0;
          return salary + partTimeIncome + bonus + otherIncome;
        },
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '工资',
      field: 'salary',
    },
    {
      label: '兼职收入',
      field: 'partTimeIncome',
    },
    {
      label: '奖金',
      field: 'bonus',
    },
    {
      label: '其他收入',
      field: 'otherIncome',
    },
    {
      label: '合计收入',
      field: 'totalIncome',
    },
    {
      label: '年度',
      field: 'year',
      render: (val) => formatDate(val, 'YYYY') as string,
    },
    {
      label: '是否有效（1：有效，0：无效）',
      field: 'statusName',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'year',
      label: '年度',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerSalaryApi.Salary>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'year',
      title: '年度',
      minWidth: 120,
    },
    {
      field: 'salary',
      title: '工资',
      minWidth: 120,
    },
    {
      field: 'partTimeIncome',
      title: '兼职收入',
      minWidth: 120,
    },
    {
      field: 'bonus',
      title: '奖金',
      minWidth: 120,
    },
    {
      field: 'otherIncome',
      title: '其他收入',
      minWidth: 120,
    },
    {
      field: 'totalIncome',
      title: '合计收入',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
